﻿namespace Hidistro.UI.SaleSystem.Tags
{
    using Hidistro.UI.Common.Controls;
    using System;
using System.Web.UI.WebControls;

    public class Common_js_Huodong : AscxTemplatedWebControl
    {
        private Literal li;
        public string productsuk { get; set; }
        protected override void AttachChildControls()
        {
            this.li = (Literal)this.FindControl("litpro");

                    string Title = "费洛蒙香水原装正品进口即喷即效,费洛蒙香水2015特惠,订购有礼!费洛蒙香水 ";
        string Keywords = "费洛蒙香水正品价格,费洛蒙香水";
        string Description = "费洛蒙香水-美国科学家从人类汗液中研发出费洛蒙香水,它对异性有强烈的吸引作用正品费洛蒙香水喷即心动,助你散发10倍体香,轻轻喷一喷就让您享受爱的情趣!";
        PageTitle.AddSiteNameTitle(Title);
        MetaTags.AddMetaKeywords(Keywords, Hidistro.Membership.Context.HiContext.Current.Context);
        MetaTags.AddMetaDescription(Description, Hidistro.Membership.Context.HiContext.Current.Context);
            post();
        }

        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "/ascx/tags/jsNewTags/Skin-Common_js_Huodong.ascx";
            }
            base.OnInit(e);
        }
    private void post()
    {
        if (ValidateCreateOrder())
        {
            Hidistro.Entities.Sales.ShoppingCartInfo shoppingCart = Hidistro.SaleSystem.Shopping.ShoppingCartProcessor.GetShoppingCart(Request.Form["product"], 1, true);
            if (Request.Form["fkfs"] == "2" || Request.Form["fkfs"] == "7")
            {
                shoppingCart.ReducedPromotionAmount = shoppingCart.GetTotal() * 0.08M;
                shoppingCart.IsReduced = true;
            }
            Hidistro.Entities.Orders.OrderInfo orderInfo = GetOrderInfo(shoppingCart);
            if (Hidistro.SaleSystem.Shopping.ShoppingProcessor.CreatOrder(orderInfo))
            {
                Hidistro.Messages.Messenger.OrderCreated(orderInfo, Hidistro.Membership.Context.HiContext.Current.User);
                orderInfo.OnCreated();
                Response.Redirect(Hidistro.Core.Globals.GetSiteUrls().UrlData.FormatUrl("FinishOrder", new object[] { orderInfo.OrderId }));
            }
        }
    }
    private Hidistro.Entities.Orders.OrderInfo GetOrderInfo(Hidistro.Entities.Sales.ShoppingCartInfo shoppingCartInfo)
    {
        Hidistro.Entities.Orders.OrderInfo orderInfo = Hidistro.SaleSystem.Shopping.ShoppingProcessor.ConvertShoppingCartToOrder(shoppingCartInfo, false, false, true);
        if (orderInfo == null)
        {
            return null;
        }
        orderInfo.OrderId = this.GenerateOrderId();
        orderInfo.OrderDate = DateTime.Now;
        Hidistro.Membership.Core.IUser user = Hidistro.Membership.Context.HiContext.Current.User;
        orderInfo.UserId = user.UserId;
        orderInfo.Username = user.Username;
        orderInfo.Remark = Hidistro.Core.Globals.HtmlEncode(Request.Form["guest"]);
        orderInfo.OrderStatus = Hidistro.Entities.Orders.OrderStatus.WaitBuyerPay;
        orderInfo.RefundStatus = Hidistro.Entities.Orders.RefundStatus.None;
        FillOrderShippingMode(orderInfo, shoppingCartInfo);
        FillOrderPaymentMode(orderInfo);
        orderInfo.OrderSource = Hidistro.Entities.Orders.OrderSource.PC;
        return orderInfo;
    }
    private void FillOrderShippingMode(Hidistro.Entities.Orders.OrderInfo orderInfo, Hidistro.Entities.Sales.ShoppingCartInfo shoppingCartInfo)
    {
        orderInfo.ShippingRegion = this.dropRegions.SelectedRegions;
        orderInfo.Address = Hidistro.Core.Globals.HtmlEncode(Request.Form["shdz"]);
        orderInfo.ShipTo = Hidistro.Core.Globals.HtmlEncode(Request.Form["shr"]);
        orderInfo.CellPhone = Request.Form["sjhm"];
        orderInfo.ShippingModeId = int.Parse("2", System.Globalization.NumberStyles.None);
        if (this.dropRegions.GetSelectedRegionId().HasValue)
        {
            orderInfo.RegionId = this.dropRegions.GetSelectedRegionId().Value;
        }
        orderInfo.ShipToDate = "时间不限";
        Hidistro.Entities.Sales.ShippingModeInfo shippingMode = Hidistro.SaleSystem.Shopping.ShoppingProcessor.GetShippingMode(orderInfo.ShippingModeId, true);
        if (shippingMode != null)
        {
            orderInfo.ShippingModeId = shippingMode.ModeId;
            orderInfo.ModeName = shippingMode.Name;
            if (!(shoppingCartInfo.IsFreightFree || ((shoppingCartInfo.LineItems.Count<Hidistro.Entities.Sales.ShoppingCartItemInfo>(a => !a.IsfreeShipping) <= 0) && (shoppingCartInfo.LineGifts.Count <= 0))))
            {
                orderInfo.AdjustedFreight = orderInfo.Freight = Hidistro.SaleSystem.Shopping.ShoppingProcessor.CalcFreight(orderInfo.RegionId, shoppingCartInfo.Weight, shippingMode);
            }
            else
            {
                orderInfo.AdjustedFreight = orderInfo.Freight = 0M;
            }
        }
    }
    private void FillOrderPaymentMode(Hidistro.Entities.Orders.OrderInfo orderInfo)
    {
        orderInfo.PaymentTypeId = int.Parse(Request.Form["fkfs"]);
        Hidistro.Entities.Sales.PaymentModeInfo paymentMode = Hidistro.SaleSystem.Shopping.ShoppingProcessor.GetPaymentMode(orderInfo.PaymentTypeId);
        if (paymentMode != null)
        {
            orderInfo.PaymentType = Hidistro.Core.Globals.HtmlEncode(paymentMode.Name);
            orderInfo.PayCharge = Hidistro.SaleSystem.Shopping.ShoppingProcessor.CalcPayCharge(orderInfo.GetTotal(), paymentMode);
            orderInfo.Gateway = paymentMode.Gateway;
        }
    }
    private string GenerateOrderId()
    {
        string str = string.Empty;
        Random random = new Random();
        for (int i = 0; i < 7; i++)
        {
            int num = random.Next();
            str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
        }
        int v = random.Next(0, 9);
        return (DateTime.Now.ToString("yyyyMMdd") + str + v);
    }
    private void ShowMessage(string msg)
    {
        this.msg.Visible = true;
        this.msg.Attributes.Add("title", msg);
    }
    private bool ValidateCreateOrder()
    {
        if (!(this.dropRegions.GetSelectedRegionId().HasValue && (this.dropRegions.GetSelectedRegionId().Value != 0)))
        {
            ShowMessage("请选择收货地址");
            return false;
        }
        string pattern = @"[\u4e00-\u9fa5a-zA-Z]+[\u4e00-\u9fa5_a-zA-Z0-9]*";
        Regex regex = new Regex(pattern);
        if (!(!string.IsNullOrEmpty(Request.Form["shr"]) && regex.IsMatch(Request.Form["shr"].Trim())))
        {
            ShowMessage("请输入正确的收货人姓名");
            return false;
        }
        if ((Request.Form["shr"].Length < 2) || (Request.Form["shr"].Length > 20))
        {
            ShowMessage("收货人姓名长度应在2-20个字符之间");
            return false;
        }
        if (string.IsNullOrEmpty(Request.Form["shdz"]))
        {
            ShowMessage("请输入收货人详细地址");
            return false;
        }
        if (string.IsNullOrEmpty(Request.Form["fkfs"]))
        {
            ShowMessage("请选择支付方式");
            return false;
        }
        if (string.IsNullOrEmpty(Request.Form["sjhm"].Trim()))
        {
            ShowMessage("请输入正确的手机号码");
            return false;
        }
        return true;
    }
</script>
    }
}

